package com.bnz.service;

import com.bnz.entity.Users;
import com.bnz.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import sun.text.normalizer.ICUBinary;

import java.util.Collection;
@Service
public class MyDetailService implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;
//    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//         if(!"admin".equalsIgnoreCase(username)){
//         	throw new UsernameNotFoundException("用户名不存在！");
//         }
//       String password = "$2a$10$tXxicD3rbbc16UdW97cwnuj7PJlIQIa/V8DLkcWu2G7qFE4N3Px1e";
        Users users1 = new Users();

        users1.setUsername(username);

        Users users = userMapper.selectOne(users1);

        Collection<? extends GrantedAuthority> authorities= AuthorityUtils.commaSeparatedStringToAuthorityList("admin");
        return new User(username,users.getPassword(),authorities);
    }
}
